﻿using Lightworks.Core.Entity;
using Lightworks.Core.Repository;
using Lightworks.Core.Service;
using Lightworks.System.Dto;
using Lightworks.System.Models;
using System.Text.Json;

namespace Lightworks.System.Services.Impl
{
    [Service]
    public class ResourceService : IResourceService
    {
        SugarRepository<Resource> repository;
        SugarRepository<RoleRes> roleResRepo;  
        IUserService userService;
        public ResourceService(IUserService userService, 
            SugarRepository<Resource> repository, SugarRepository<RoleRes> roleResRepo) 
        {
            this.repository = repository;
            this.roleResRepo = roleResRepo;
            this.userService = userService;
        }

        /// <summary>
        /// 获取用户菜单
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public MyResourceRes GetResourceByUser(string username)
        {

            MyResourceRes res = new MyResourceRes();
            var user = userService.GetUser(username);
            var data = repository.GetList(r=>!r.IsHidden);
            if (user.IsSuper) 
            {
                res.Menu = data.List2Tree();
                res.Permissions = new List<string> { "*" };
                res.DashboardGrid = new List<string> { "*" };
                return res;
            }

            if (user.Roles ==null)
                return res;

            List<long> roleIds = new List<long>();
            user.Roles.ForEach(r => {
                roleIds.Add(r.Id);
                if(r.ConsoleArea != null)
                    res.DashboardGrid.AddRange(r.ConsoleArea);
            });

            var roleResList = roleResRepo.GetList(rr => roleIds.Contains(rr.RoleId));
            data.ForEach(row => {
                if (roleResList.Exists(r => r.ResourceId == row.Id))
                {

                    if (row.Meta!.Type == "button")
                    {
                        res.Permissions.Add(row.Name!);
                    }
                    else
                    {
                        res.Menu.Add(row);
                    }
                }
            });
            res.Menu = res.Menu.List2Tree();
            return res;
        }

        /// <summary>
        /// 获取用户权限
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public List<string> GetPermsByUser(User user) 
        {
            return null;
        }

        public List<Resource> GetAll()
        {
            return repository.GetList().List2Tree();
        }

        public long Save(Resource resource)
        {
            if (resource.Meta != null) 
            {
                resource.IsHidden = resource.Meta.Hidden;
            }
            if (resource.Id == 0)
            {
                resource.Id = repository.InsertReturnBigIdentity(resource);
            }
            else {
                repository.Update(resource);
            }
            return resource.Id;
        }

        public bool Delete(long[] ids)
        {
            return repository.Delete(x=>ids.Contains(x.Id));
        }
    }
}
